import { API } from '@/constants/api';
import { portal as saveLoading } from 'src/components/Project/modals/SaveLoading.vue';
import { LocalFileService } from 'src/util/localFile/LocalFileService';
import { SysMLUtilHelper, MofModelRepositoryHelper } from "src/api/sysml-imports";
import graphNodeStatusFresher from '../GraphNodeStatusFresher';

class UpdateModel {
  updateProject(){
    saveLoading.popup({
      time: 2,
      msg: '正在下载更新',
      saveFn: () => this.getProjectData()
    });
  }
  getProjectData(){
    app.$get(API.PLATFORM_SERVICE_PROJECT_DOWNLOAD, { _id: app.activeProject.config.id }, { responseType: 'arraybuffer' })
      .then((res) => {
        this.updateModelData(res.data);
      })
      .catch((err) => {
        throw err;
        //// app.tipError({ status: 6011 });
      });
  }
  async updateModelData(project){
    const config = app.activeProject.config;
    const projectId = app.activeProject.config.id;
    await this.deleteProject(projectId);
    
    LocalFileService.loadProjectData(project).then((res) => {
      LocalFileService.switchToProject(res);
      //// 为解决路径不一致问题
      app.activeProject.config  = config; 
      app.$messageBox.success({
        text: '更新成功',
        duration: 3000,
      });
      graphNodeStatusFresher.freshNodeStatus();
      app.$bus.emit('project-modified');
      app.activeProject.onlineConfig.needSubmit = false;
    }).catch((err) => {
      console.log(err);
    });
  }
  deleteProject(projectId) {
    app.proxy.projects.delete(projectId);
    //// SysMLUtilHelper.removePoolById(projectId);
    MofModelRepositoryHelper.removePool(projectId);
  }
}

const updateModel = new UpdateModel();
export default updateModel;
